<?php

namespace App\Modules\Admin\Services\Water\Apparatus;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Water\ApparatusPropertyFreeModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class PropertyFeeService extends BaseService
{
    public function __construct(ApparatusPropertyFreeModel $model)
    {
        $this->model = $model;
    }
    public function updatePropertyFee($params)
    {
        $id = $params['id'];
        $price = $params['price'];
        $startedAt = date("Y-m-d",time());
        $endedAt = date("Y-m-d",strtotime('+1 year'));
        $insert_data=[
            'apparatus_id'=>$id,
            'price'=>$price*100,
            'type'=>'year',
            'started_at'=>$startedAt,
            'ended_at'=>$endedAt,
        ];

        DB::beginTransaction();
        try{
            $detail = parent::create($insert_data); // TODO: Change the autogenerated stub
            exit(json_encode($detail));
            $detail->propertyFreeInfo()->create([
                'property_fee_time' => $endedAt,
                'property_fee' => $price*100,
            ]);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
}
